package com.etl.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.etl.enums.ResultEnum;
import com.etl.exception.GlobalException;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

@Service
public class HxSendMessageUtil {

    public static final String sender = "ETL任务管理系统";
    private static final String eid = "103";
    private static final String hxXtHost = "http://weixin.hongkun.com.cn";
    private static final String notifyUrl = "http://hxweb.hongkun.com.cn/broadcast/notify.jsp";
    private static final String pubacc = "XT-c7bc8b8f-0a43-49d0-ab2c-75d36952bd46";
    private static final String pubaccKey = "ab9a2c964bb8de7acad0ceef353d99d1";
    private static final String appId = "1033492";
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 发送鸿信消息,单点或者广播都可以
     *
     * @param outerTitle 列表标题
     * @param outerText  列表内容
     * @param userId     接收人
     * @param innerTitle 内部标题
     * @param innerText  内部内容
     */
    @Async
    public void sendHxNotifyMsg(String outerTitle, String outerText, Object[] userId, String innerTitle, String innerText) {
        if (userId == null || userId.length == 0) {
            throw new GlobalException(ResultEnum.PARAMS_ERROR);
        }
        List<Object> toUserList = Arrays.asList(userId);
        try {
            String random = String.valueOf(Math.random() * 100);
            long timestamp = System.currentTimeMillis();
            // form
            JSONObject from = new JSONObject();
            from.put("no", eid);
            from.put("pub", pubacc);
            from.put("time", timestamp);
            from.put("nonce", random);
            String[] data = {eid, pubacc, pubaccKey, random, String.valueOf(timestamp)};
            Arrays.sort(data);
            from.put("pubtoken", DigestUtils.shaHex(StringUtils.join(data)));
            // to
            JSONArray tos = new JSONArray();
            JSONObject to = new JSONObject();
            to.put("no", eid);
            to.put("user", toUserList);
            to.put("code", 2);
            tos.add(to);
            JSONObject msg = new JSONObject();
            innerTitle = URLEncoder.encode(innerTitle, "UTF-8");
            innerText = URLEncoder.encode(innerText, "UTF-8");
            msg.put("url", new StringBuilder(notifyUrl).append("?title=").append(innerTitle).append("&text=")
                    .append(innerText).append("&sender=").append(URLEncoder.encode(sender, "UTF-8")).append("&pubtime=").append(DateUtil.getCurrent())
                    .toString());
            msg.put("appid", appId);
            //todo:是否推送代表消息,格式为整型,默认1=推送到待办消息;0=推送原公共号消息,
            msg.put("todo", 1);
            msg.put("todoPriStatus", "notify");
            msg.put("sourceid", System.currentTimeMillis() + "");
            msg.put("text", outerText);
            JSONObject content = new JSONObject();
            content.put("from", from);
            content.put("to", tos);
            content.put("type", 5);
            content.put("msg", msg);
            String contentStr = content.toString();
            System.out.println(contentStr);
            String rs = restTemplate.postForObject(hxXtHost + "/pubacc/pubsend", content, String.class);
            System.out.println("通知发送结果---" + rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
